
let gfx = cc.gfx;

window.MeshHelper =
    cc.Class({
        name: "window.MeshHelper",
        extends: cc.Component,
        editor: {
            executeInEditMode: true
        },
        properties: {
            mr: cc.MeshRenderer,
            handles: cc.Node,
            _debug: false,
            isDebug: {
                get() {
                    return this._debug;
                },
                set(val) {
                    this._debug = val;
                    if (this._debug) {
                        for (let i = 0; i < this.handles.childrenCount; i++) {
                            var node = this.handles.children[i];
                            var lb = node.addComponennt(cc.Label);
                            lb.string = i + 1;
                            lb.fontSize = 25;
                        }
                    }
                    else {
                        for (let i = 0; i < this.handles.childrenCount; i++) {
                            var node = this.handles.children[i];
                            var lb = node.getComponennt(cc.Label);
                            if (lb) {
                                lb.destroy();
                            }
                        }
                    }
                }
            }
        },

        onLoad() {
            for (let i = 0; i < this.handles.childrenCount; i++) {
                this.handles.children[i].on("position-changed", this.updateVec, this);
            }
            if( CC_EDITOR ) {
                this.node.on("size-changed", this.reset, this);
            }
        },
        start(){
            this.reset();
        },
        reset() {
            this.mr.mesh = null;
            this.mesh = null;
            var vfmt = new gfx.VertexFormat([
                { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3 },
                { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
            ]);
            var vecLength = this.handles.childrenCount;
            //更新点位置。
            this.handles.children[0].x = -this.node.width / 2;
            this.handles.children[0].y = this.node.height / 2;

            this.handles.children[1].x = this.node.width / 2;
            this.handles.children[1].y = this.node.height / 2;

            this.handles.children[2].x = -this.node.width / 2;
            this.handles.children[2].y = -this.node.height / 2;

            this.handles.children[3].x = this.node.width / 2;
            this.handles.children[3].y = -this.node.height / 2;

            let mesh = new cc.Mesh();
            mesh.init(vfmt, vecLength, true);
            this.mesh = mesh;

            // 0, 1
            // 2, 3
            var indices = [
                0, 2, 3,
                1, 0, 3
            ];
            mesh.setIndices(indices);

            mesh.setVertices(gfx.ATTR_UV0, [
                cc.v2(0, 0), cc.v2(1, 0),
                cc.v2(0,1), cc.v2(1, 1),
            ]);

            this.updateVec();
            this.mr.mesh = mesh;

            cc.log("reset");
        },
        updateVec() {
            if (!this.mesh) {
                return;
            }

            cc.log("updateVec");

            var vecs = [];
            for (let i = 0; i < this.handles.childrenCount; i++) {
                vecs.push(this.handles.children[i].position);
            }
            this.mesh.setVertices(gfx.ATTR_POSITION, vecs);

        },

        // update(dt) {
        //     if (!CC_EDITOR) return;
        //     this.updateVec();
        // },
    });
